package org.zht.dormpro.dorm.controller;

import lombok.extern.log4j.Log4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.MajorDTO;
import org.zht.dormpro.dorm.model.TbCollege;
import org.zht.dormpro.dorm.model.TbMajor;
import org.zht.dormpro.dorm.service.ICollegeService;
import org.zht.dormpro.dorm.service.IMajorService;
import org.zht.dormpro.pub.ResponseMap;

import java.io.UnsupportedEncodingException;
import java.util.*;

@Log4j
@Controller
@RequestMapping("/dorm/major")
public class MajorController {

    @Autowired
    private IMajorService majorService;
    @Autowired
    private ICollegeService collegeService;

    /**
     * 跳转到专业页面
     * @param model
     * @return
     */
    @RequestMapping("/majorPage")
    public String majorPage(Model model){
        List<TbCollege> colleges = collegeService.getAllCollege(0);
        model.addAttribute("colleges",colleges);
        return "/major/major_list";
    }


    /**
     * 获取专业信息
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/listMajor")
    @RequiresPermissions("dorm:list:all")
    public ResponseMap listMajor(@RequestParam Map<String,Object> params){
        try {
            String majName = new String(params.get("majName").toString().getBytes("iso-8859-1"),"UTF-8");
            params.put("majName",majName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        PageBean pageBean = new PageBean(params);
        List<MajorDTO> majorDTOS = majorService.getMajorByCondition(pageBean);
        Map<String, Object> attr = new HashMap<>();
        attr.put("total", pageBean.getTotal());
        return ResponseMap.success(majorDTOS, attr);
    }

    /**
     * 跳转到添加页面
     * @param model
     * @return
     */
    @RequestMapping("addMajorPage")
    public String addMajorPage(Model model){
        model.addAttribute("colleges",collegeService.getAllCollege(0));

        return "/major/major_add";
    }

    /**
     * 添加专业信息
     * @param majorDTO
     * @return
     */
    @RequestMapping(value="/addMajor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap addMajor(MajorDTO majorDTO){
        boolean success = majorService.addMajor(majorDTO);
        if(success){
            return ResponseMap.success("添加专业成功");
        }else{
            return ResponseMap.error("添加专业失败");
        }
    }

    /**
     * 删除专业信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteMajor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap deleteMajor(Integer id){
        boolean success = majorService.deleteMajor(id);
        if(success){
            return ResponseMap.success("删除专业成功");
        }else {
            return ResponseMap.error("删除专业失败");
        }
    }

    /**
     * 批量删除专业
     * @param majIds
     * @return
     */
    @RequestMapping(value = "/batchRemoveMajor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap batchRemoveMajor(@RequestParam("majIds[]") String[] majIds){
        boolean success = majorService.batchRemoveMajor(new ArrayList<String>(Arrays.asList(majIds)));
        if(success){
            return ResponseMap.success("批量删除专业成功");
        }else {
            return ResponseMap.error("批量删除专业失败");
        }
    }

    /**
     * 跳转到编辑页面
     * @param model
     * @return
     */
    @RequestMapping("/majorEditPage/{id}")
    public String majorEditPage(Model model, @PathVariable("id") Integer id){
        TbMajor tbMajor = majorService.getMajorById(id);
       List<TbCollege> colleges = collegeService.getAllCollege(0);
        model.addAttribute("colleges",colleges);
        model.addAttribute("major",tbMajor);
        return "/major/major_edit";
    }

    /**
     * 编辑专业信息
     * @param majorDTO
     * @return
     */
    @RequestMapping(value = "/editMajor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap editMajor(MajorDTO majorDTO){
        log.info(majorDTO);
        boolean success = majorService.editMajor(majorDTO);
        if (success){
            return ResponseMap.success("修改专业信息成功");
        }else {
            return ResponseMap.error("修改专业信息失败");
        }
    }
}
